package cn.iocoder.yudao.module.trade.service.depositorderlackbottlesrelation;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.trade.controller.admin.depositorderlackbottlesrelation.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderlackbottlesrelation.DepositOrderLackBottlesRelationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.depositorderlackbottlesrelation.DepositOrderLackBottlesRelationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 欠瓶记录对应瓶数据 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DepositOrderLackBottlesRelationServiceImpl implements DepositOrderLackBottlesRelationService {

    @Resource
    private DepositOrderLackBottlesRelationMapper depositOrderLackBottlesRelationMapper;

    @Override
    public Long createDepositOrderLackBottlesRelation(DepositOrderLackBottlesRelationSaveReqVO createReqVO) {
        // 插入
        DepositOrderLackBottlesRelationDO depositOrderLackBottlesRelation = BeanUtils.toBean(createReqVO, DepositOrderLackBottlesRelationDO.class);
        depositOrderLackBottlesRelationMapper.insert(depositOrderLackBottlesRelation);
        // 返回
        return depositOrderLackBottlesRelation.getId();
    }

    @Override
    public void updateDepositOrderLackBottlesRelation(DepositOrderLackBottlesRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateDepositOrderLackBottlesRelationExists(updateReqVO.getId());
        // 更新
        DepositOrderLackBottlesRelationDO updateObj = BeanUtils.toBean(updateReqVO, DepositOrderLackBottlesRelationDO.class);
        depositOrderLackBottlesRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteDepositOrderLackBottlesRelation(Long id) {
        // 校验存在
        validateDepositOrderLackBottlesRelationExists(id);
        // 删除
        depositOrderLackBottlesRelationMapper.deleteById(id);
    }

    private void validateDepositOrderLackBottlesRelationExists(Long id) {
        if (depositOrderLackBottlesRelationMapper.selectById(id) == null) {
            throw exception(DEPOSIT_ORDER_LACK_BOTTLES_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public DepositOrderLackBottlesRelationDO getDepositOrderLackBottlesRelation(Long id) {
        return depositOrderLackBottlesRelationMapper.selectById(id);
    }

    @Override
    public PageResult<DepositOrderLackBottlesRelationDO> getDepositOrderLackBottlesRelationPage(DepositOrderLackBottlesRelationPageReqVO pageReqVO) {
        return depositOrderLackBottlesRelationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DepositOrderLackBottlesRelationRespVO> getDepositOrderRelationByLackId(Long id) {
        LambdaQueryWrapperX<DepositOrderLackBottlesRelationDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.eq(DepositOrderLackBottlesRelationDO::getLackBottlesId, id);
        List<DepositOrderLackBottlesRelationDO> relationDOList = depositOrderLackBottlesRelationMapper.selectList(wrapper);

        return BeanUtils.toBean(relationDOList, DepositOrderLackBottlesRelationRespVO.class);
    }

    @Override
    public void insertBathRelation(List<DepositOrderLackBottlesRelationRespVO> relationRespVOList) {
        depositOrderLackBottlesRelationMapper.insertBatch(BeanUtils.toBean(relationRespVOList, DepositOrderLackBottlesRelationDO.class));
    }

}